/**
 * 
 */
package com.wy2.generator.database.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.xo68.core.util.StringTools;
import com.fasterxml.jackson.annotation.JsonIgnore;


/**
 * 数据库原始表结构
 * @author nnsword
 * @date 2017-12-11 22:33:53
 *
 */
public class DatabaseTable  extends DatabaseElement implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String catalog;
	private String schema;
	private String module;
	private String prefix;
	private String sqlName;
	private String remarks;
	private String tableType;
	private String entityName;
	
	private List<String> primaryKey;
	
	private List<DatabaseColumn> columns;
	
	
	/**
	 * @return the catalog
	 */
	public String getCatalog() {
		return catalog;
	}
	/**
	 * @param catalog the catalog to set
	 */
	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}
	/**
	 * @return the schema
	 */
	public String getSchema() {
		return schema;
	}
	/**
	 * @param schema the schema to set
	 */
	public void setSchema(String schema) {
		this.schema = schema;
	}
	/**
	 * @return the module
	 */
	public String getModule() {
		return module;
	}
	/**
	 * @param module the module to set
	 */
	public void setModule(String module) {
	   this.module= StringTools.isEmpty(module) ? "" : module.toLowerCase();
	}
	/**
	 * @return the prefix
	 */
	public String getPrefix() {
		return prefix;
	}
	/**
	 * @param prefix the prefix to set
	 */
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
	/**
	 * @return the sqlName
	 */
	public String getSqlName() {
		return sqlName;
	}
	/**
	 * @param sqlName the sqlName to set
	 */
	public void setSqlName(String sqlName) {
		this.sqlName = sqlName;
	}
	/**
	 * @return the remarks
	 */
	public String getRemarks() {
		return remarks;
	}
	/**
	 * @param remarks the remarks to set
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	
	
	
	
	/**
	 * @return the tableType
	 */
	public String getTableType() {
		return tableType;
	}
	/**
	 * @param tableType the tableType to set
	 */
	public void setTableType(String tableType) {
		this.tableType = tableType;
	}
	
	/**
	 * @return the entityName
	 */
	public String getEntityName() {
		return entityName;
	}
	/**
	 * @param entityName the entityName to set
	 */
	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}
	
	
	
	/**
	 * @return the columns
	 */
	public List<DatabaseColumn> getColumns() {
		return columns;
	}
	
	/**
	 * @param columns the columns to set
	 */
	protected void setColumns(List<DatabaseColumn> columns) {
	    for (DatabaseColumn databaseColumn : columns) {
	        databaseColumn.setTable(this);
        }
		this.columns = columns;
	}
	public void addColumn(DatabaseColumn column) {
	    column.setTable(this);
		this.columns.add(column);
	}
	public void addColumns(List<DatabaseColumn> columns) {
		this.columns.addAll(columns);
	}	
	@JsonIgnore
	public DatabaseColumn getPkColumn() {
		for(DatabaseColumn column : this.columns) {
			if(StringTools.contains(column.getSqlName(), this.getPrimaryKey().toArray(new String[this.getPrimaryKey().size()]))) {
				return column;
			}
		}
		return null;
	}
	@JsonIgnore
	public Integer getColumnCount() {
	    return this.columns.size();
	}

	/**
	 * @return the primaryKey
	 */
	public List<String> getPrimaryKey() {
		return primaryKey;
	}
	
	/**
	 * @param primaryKey the primaryKey to set
	 */
	protected void setPrimaryKey(List<String> primaryKey) {
		this.primaryKey = primaryKey;
	}
	public void addPrimaryKey(String primaryKey) {
		this.primaryKey.add(primaryKey);
	}
	public void addPrimaryKeys(List<String> primaryKeys) {
		this.primaryKey.addAll(primaryKeys);
	}
	
	
	/**
	 * 
	 */
	public DatabaseTable() {
		super();
		this.columns=new ArrayList<DatabaseColumn>();
		this.primaryKey=new ArrayList<String>();
	}
	
	
	
	
	/**
	 * @param schema
	 * @param module
	 * @param sqlName
	 * @param remarks
	 * @param tableType
	 */
	public DatabaseTable(String catalog,String schema, String module,  String sqlName, String remarks,
			String tableType) {
		this();
		this.catalog=catalog;
		this.schema = schema;
		this.module = StringTools.isEmpty(module) ? "" : module.toLowerCase();
		this.sqlName = sqlName;
		this.remarks = remarks;
		this.tableType = tableType;
	}
	
	
	


	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "DatabaseTable [catalog=" + catalog + ", schema=" + schema + ", module=" + module + ", prefix=" + prefix
				+ ", sqlName=" + sqlName + ", remarks=" + remarks + ", tableType=" + tableType + ", entityName="
				+ entityName + ", primaryKey=" + primaryKey + ", columns=" + columns + "]";
	}
	
	public void checkedAll(Boolean checked) {
	    for (DatabaseColumn databaseColumn : columns) {
	        databaseColumn.setChecked(checked);
        }
	}
	
	
	/**
	 * 标记主键字段，如果是联合主键，会标记联合的所有字段
	 * @date 2017-12-13 22:12:41
	 */
	public void markPrimaryKey() {
		this.columns.forEach(column->{
			if(this.primaryKey.contains(column.getSqlName())) {
				column.setPk(true);
			}
		});
	}
	
	
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((sqlName == null) ? 0 : sqlName.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DatabaseTable other = (DatabaseTable) obj;
        if (sqlName == null) {
            if (other.sqlName != null)
                return false;
        } else if (!sqlName.equals(other.sqlName))
            return false;
        return true;
    }
	
	
	

}
